package logic

import (
	"context"
	"doctor-medicine-system/models/mysql"
	"errors"

	"doctor-medicine-system/doctorrpc/doctorrpc"
	"doctor-medicine-system/doctorrpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type MedicineAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewMedicineAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MedicineAddLogic {
	return &MedicineAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *MedicineAddLogic) MedicineAdd(in *doctorrpc.MedicineAddRequest) (*doctorrpc.MedicineAddResponse, error) {
	// todo: add your logic here and delete this line
	medicine := mysql.Medicine{
		Name:     in.Name,
		GuiGe:    in.GuiGe,
		ShangJia: in.ShangJia,
		ShiZheng: in.ShiZheng,
		Num:      in.Num,
		DoctorId: in.DoctorId,
		Types:    in.Types,
	}
	err := medicine.MedicineFindByName(mysql.DB, in.Name)
	if err != nil {
		return nil, errors.New("已存在这个物品，添加失败")
	}
	return &doctorrpc.MedicineAddResponse{
		MedicineId: int64(medicine.ID),
	}, nil
}
